﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DingY.DB;

namespace DotNet.WinUI.Admin
{
    public partial class FrmUsersEdit : BaseForm
    {
        public FrmUsersEdit()
        {
            InitializeComponent();
        }

        /// <summary>
        ///  0表示为新增，否则为编辑
        /// </summary>
        private string _Sign;

        public FrmUsersEdit(string Sign)
        {
            InitializeComponent();

            _Sign = Sign;

            if (_Sign != "0")
            {
                string _Sql = @"select *
                    from Users
                    where LoginID = '{0}' ";
                _Sql = string.Format(_Sql, _Sign);

                SqlDB objSqlDB = new SqlDB();

                //依次读取数据
                DataRow objRow = objSqlDB.ExecuteDataTable(_Sql).Rows[0];
                //将数据库中的值在控件中显示
                txtDengLDM.Text = objRow["LoginID"].ToString();
                txtMiM.Text = objRow["Pwd"].ToString();
                txtXingM.Text = objRow["Name"].ToString();
                txtDepict.Text = objRow["Depict"].ToString();
            }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text)
            {
                case "保存":
                    SaveData();
                    break;
                case "关闭":
                    Close();
                    break;
            }
        }

        private void SaveData()
        {
            if (txtMiM.Text != txtConfirmPwd.Text)
            {
                ShowMsg("密码和确认密码不一致，请重新输入！");
                return;
            }
            string _Sql = "";
            if (_Sign == "0")
            {
                _Sql = @"insert into Users(LoginID,
	                            Name,
	                            Depict,
	                            Pwd)
                            values ('{0}',
	                            '{1}',
	                            '{2}',
	                            '{3}'
                            )";
            }
            else
            {
                _Sql = @"update Users
                            Set Name ='{1}', 
	                            Depict='{2}'
                            where LoginID ='{0}' ";
            }
            _Sql = string.Format(_Sql,
                txtDengLDM.Text,
                txtXingM.Text,
                txtDepict.Text,
                txtMiM.Text);

            SqlDB objSqlDB = new SqlDB();
            if (objSqlDB.ExecuteSql(_Sql) >= 1)
            {
                ShowMsg("数据保存成功!");
            }
        }
    }
}